package com.like.hospitalworld;

import android.os.Bundle;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.like.adapter.CourseAdapter;
import com.like.entity.GeneralResponse;
import com.like.entity.MeetingCourseListReq;
import com.like.entity.MeetingCourseListResp;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.MeetingService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_courseware)
public class CoursewareActivity extends BaseActivity {

    @ViewInject(R.id.course_list)
    private PullToRefreshListView mCourseList;

    private CourseAdapter mCourseAdapter;
    private MeetingService mMeetingService;

    private int mMeetingId;
    private int mCurrentPage = 0;
    private int mPageCount = 15;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mMeetingService = RetrofitUtil.getService(MeetingService.class);
        mMeetingId = getIntent().getIntExtra("meetingId", -1);
        initList();
        getMeetingCourseList(false);
    }

    private void initList() {
        mCourseList.setMode(PullToRefreshBase.Mode.BOTH);
        mCourseList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage = 0;
                getMeetingCourseList(false);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPage += 1;
                getMeetingCourseList(true);
            }
        });
    }

    private void getMeetingCourseList(final boolean append) {
        MeetingCourseListReq req = new MeetingCourseListReq(2, mMeetingId, mCurrentPage, mPageCount);
        Call<GeneralResponse<MeetingCourseListResp>> call = mMeetingService.getMeetingCourseList(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<MeetingCourseListResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<MeetingCourseListResp>> call, Response<GeneralResponse<MeetingCourseListResp>> response) {
                mCourseList.onRefreshComplete();
                MeetingCourseListResp resp = response.body().data;
                List<MeetingCourseListResp.MeetingCourse> courses = resp.meetingInformationList;
                if (mCourseAdapter == null) {
                    mCourseAdapter = new CourseAdapter(courses, m);
                    mCourseList.setAdapter(mCourseAdapter);
                } else {
                    mCourseAdapter.update(courses, append);
                }
            }

            @Override
            public void onFailure(Call<GeneralResponse<MeetingCourseListResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
